package num200;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/9 20:08
 * @Desc :
 */
public class Solution {
  public int numIslands(char[][] grid) {
    if (grid == null || grid.length == 0) return 0;
    int height = grid.length;
    int width = grid[0].length;
    int res = 0;
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        if (grid[i][j] == '1') {
          res++;
          dfs(grid, i, j);
        }
      }
    }
    return res;
  }

  public void dfs(char[][] grid, int h, int w) {
    if (h < 0 || w < 0 || w >= grid[0].length || h >= grid.length || grid[h][w] == '0') return;

    grid[h][w] = '0';
    dfs(grid, h - 1, w);
    dfs(grid, h + 1, w);
    dfs(grid, h, w - 1);
    dfs(grid, h, w + 1);
  }

  public static void main(String[] args) {
    char[][] grid = {
            {'1', '1', '0', '0', '0'},
            {'1', '1', '0', '0', '0'},
            {'0', '0', '1', '0', '0'},
            {'0', '0', '0', '1', '1'}
    };
    int res = new Solution().numIslands(grid);
    System.out.println(res);
  }
}
